/*
 * @Author: 涵崽
 * @Date: 2023-12-04 15:05:11
 * @email: 1045654@qq.com
 * @gitee: https://gitee.com/han-zai
 * @LastEditors: 涵崽
 * @LastEditTime: 2023-12-05 15:18:41
 * @Description: 表单验证
 * 
 * 使用方法
 * import { validateMobile } from "@/utils/config";
 *  rules: {
        phone: [
          { required: true, message: "请输入联系人手机", trigger: "blur" },
          { validator: validateMobile.bind(this), trigger: "blur" },
        ],
      },
 */
// 手机
export function validateMobile(rule, value, callback) {
  let newValue = value.replace(/^1[3456789]\d{9}$/, "");
  if (value !== newValue) {
    callback(new Error("手机号码格式错误"));
  } else if (newValue.length !== 11) {
    callback(new Error("手机号码长度错误"));
  } else {
    callback();
  }
}
// 邮箱
export function validateEmail(rule, value, callback) {
  // QQ邮箱正则：^[1-9]\\d{4,10}@qq.com$
  var newValue = value.replace(/^(\\w)+(\\.\\w+)*@(\\w)+((\\.\\w+)+)$/, "");
  if (value !== newValue) {
    callback(new Error("邮箱码格式错误"));
  } else {
    callback();
  }
}
// 账号
export function validateUsername(rule, value, callback) {
  if (value.length <= 0) {
    callback(new Error("请输入账号"));
  } else {
    callback();
  }
}
// 密码
export function validatePassword(rule, value, callback) {
  if (value.length < 6) {
    callback(new Error("密码长度不能低于6位"));
  } else {
    callback();
  }
}
// 身份证
export function checkCode(rule, value, callback) {
  var p =
    /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
  var factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
  var parity = [1, 0, "X", 9, 8, 7, 6, 5, 4, 3, 2];
  var code = value.substring(17);
  if (p.test(value)) {
    var sum = 0;
    for (var i = 0; i < 17; i++) {
      sum += val[i] * factor[i];
    }
    if (parity[sum % 11] == code.toUpperCase()) {
      callback();
    }
  }
 callback(new Error("身份证号码格式错误"));
 }

